//
//  KLineQuartzView.m
//  AppFinance
//
//  Created by wei on 12-6-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AFKLineQuartzView.h"
#import "AFMarketData.h"
#import "AFUtils.h"

#import "AFMarketData.h"
#import "AFSetting.h"

#import "AFFingerView.h"

#define K_LINE_DRAW_MOVE_X 2 //6

//CGContextSetRGBStrokeColor(context, 0.0, 1.0, 1.0, 1.0);    // 清蓝色
//CGColorRef redColor = [[Utils UIColorFromRGB:0xff6060 alpha:1.0] CGColor];
//CGColorRef greenColor = [[Utils UIColorFromRGB:0x00ffff alpha:1.0] CGColor];
//#define K_LINE_UP_COLR 1.0, 0.0, 0.0, 1.0         // 红色
//#define K_LINE_DOWN_COLR 1.0, 0.0, 0.0, 1.0       

@implementation AFKLineQuartzView

#pragma mark - initView
-(void)initView{
    AF_KLIN_TOUCH_TYPE = AF_KLIN_TOUCH_NO;
}

#pragma mark - distory
- (void) distory {
    isDistroy = YES;
    
    [self setNeedsDisplay];
}

#pragma mark - init k line data
- (void) initKLineArray:(NSMutableArray*)array kLineType:(NSString*)type isLast:(BOOL)isLast{
    
    // 是否刷新最后一条 K Line 数据
    if (isLast) {
        if ((S_DRAW_INDEX + [AFSetting AF_DRAW_COUNT]) >= ALL_COUNT) {
            NSLog(@" udate last k line data ");
        } else {
            NSLog(@" no need udate last k line data ");
            return;
        }
    }

    VIEW_W = self.frame.size.width;
    
    isInitMA = NO;
    
    kLineArray = array;
    kLineType = type;
    
    ALL_COUNT = (int)[array count];

    S_MIN_DRAW_INDEX = 0;
    S_DRAW_INDEX = 0;
    if (ALL_COUNT > [AFSetting AF_DRAW_COUNT]) {
        S_DRAW_INDEX = ALL_COUNT - [AFSetting AF_DRAW_COUNT];
        
        // 为了 计算 MA20 数据, 所以 最左面 只 绘制到 index 为 20 的数据
//        if (ALL_COUNT > [AFSetting AF_DRAW_COUNT] + 20) {
//            S_MIN_DRAW_INDEX = 0;
//        }
    }

    F_DRAW_INDEX = S_DRAW_INDEX;

    // [S] 计算 MA 数据
    [[AFMarketData data] serverKLineDataToMALocal:kLineArray];
    
    localMA5ListArray = [[AFMarketData data] drawMA5ListArray];
    localMA10ListArray = [[AFMarketData data] drawMA10ListArray];
    localMA20ListArray = [[AFMarketData data] drawMA20ListArray];
    // [E]

    [self drawArrayRefresh];
}

// 把 将要绘制的 原始数据 转换成 本地 坐标数据
- (void) drawArrayRefresh{
    NSLog(@"ALL_COUNT = %i, start_draw_index = %i", ALL_COUNT, S_DRAW_INDEX);
    
    // 获取数据的条数
    KLINE_DRAW_COUNT = [AFSetting AF_DRAW_COUNT];
    // 如果总条数 < 默认获取的条数, 则 要绘制的条数 = 总条数
    if (ALL_COUNT < [AFSetting AF_DRAW_COUNT]) {
        KLINE_DRAW_COUNT = ALL_COUNT;
    }
    kLineDrawArray = [kLineArray subarrayWithRange:NSMakeRange(S_DRAW_INDEX, KLINE_DRAW_COUNT)];
    NSLog(@" kLineDrawArray count = %i", (int)[kLineDrawArray count]);
 
    
    
    // 烛台图数据
    [[AFMarketData data] drawMAStartIndex:S_DRAW_INDEX withDrawCount:KLINE_DRAW_COUNT];
    localKLindListArray = [[AFMarketData data] serverKLineDataToLocalKLineDataWithArray:kLineDrawArray];
    
    last_KLineHigh = [[AFMarketData data] lastKLineHigh];
    last_KLineLow = [[AFMarketData data] lastKLineLow];

    [self setNeedsDisplay];
}

// 根据手指移动 的 距离 刷新 要绘制的 K Line
- (void) touchMoveRefreshDrawArray{
    NSLog(@" start_touch_x = %f, move_touch_x = %f", start_touch_x, move_touch_x);
    
    if (start_touch_x < move_touch_x) {
        NSLog(@"向右移动");
        
        if ((move_touch_x - start_touch_x) < K_LINE_DRAW_MOVE_X) {
            return;
        }
        start_touch_x = move_touch_x;
        
        if (S_DRAW_INDEX > S_MIN_DRAW_INDEX) {
            S_DRAW_INDEX --;
            
            [self drawArrayRefresh];
            
            // 刷新 kpi UIView
            [self.kpiQuartzView touchMoveRefreshDrawArrayWithOrientation:1];
        }
    }
    else if (start_touch_x > move_touch_x) {
        NSLog(@"向左移动");
        
        if ((start_touch_x - move_touch_x) < K_LINE_DRAW_MOVE_X) {
            return;
        }
        start_touch_x = move_touch_x;
        
        if (S_DRAW_INDEX < F_DRAW_INDEX) {
            S_DRAW_INDEX ++;
            
            [self drawArrayRefresh];
            
            // 刷新 kpi UIView
            [self.kpiQuartzView touchMoveRefreshDrawArrayWithOrientation:2];
        }
    }
}

- (float) lastMa5{
    return last_ma5;
}
- (float) lastMa10{
    return last_ma10;
}
- (float) lastMa20{
    return last_ma20;
}

#pragma mark - draw UIView
-(void)drawInContext:(CGContextRef)context rect:(CGRect)rect{
    
    NSLog(@"------- StockDayQuartzView drawInContext ----");
    
    if (isDistroy) {
        isDistroy = NO;
        [self draw4distory:context];
        return;
    }
    
    // 绘制背景框
    [self drawBackGround:context];
    
    // 开始绘制烛台图
    [self drawZhuTai:context];
    
    // 开始绘制MA
    [self drawMA:context];
    
    if ([self.delegate respondsToSelector:@selector(kLineQuartzViewDataRefresh)]) {
        [self.delegate kLineQuartzViewDataRefresh];
    }
}

- (void) draw4distory:(CGContextRef)context{
    // 绘制前，先清楚所有绘图区域
    CGContextClearRect(context, CGRectMake(0, 0, [AFSetting QUARTZ_KLINE_W], [AFSetting QUARTZ_KLINE_H]));
    // 绘制背景框
    [self drawBackGround:context];
}

/*
 整个矩形框范围: 320 *242
 烛台范围: 左上角坐标(50,0) -- 右下角(320,160)
 底部图范围:  左上角坐标(50,172) -- 右下角(320,242)
 */ 

// 绘制背景框
-(void) drawBackGround :(CGContextRef)context{
    
    // 绘制前，先清楚所有绘图区域
    //CGContextClearRect(context, CGRectMake(0, 0, 320, 242));
    CGContextClearRect(context, CGRectMake(0, 0, [AFSetting QUARTZ_KLINE_W], [AFSetting QUARTZ_KLINE_H]));
    
    //CGContextSetLineCap(context, cap);
    //CGContextSetLineJoin(context, join);
    
    // [S]整个矩形框范围

    // 设置线宽
    CGContextSetLineWidth(context, 1.0f);
    CGContextSetStrokeColorWithColor(context, AF_K_QUARTZ_DRAW_BORDER_CGCOLOR);
    CGContextAddRect(context, CGRectMake(0, 0, [AFSetting QUARTZ_KLINE_W], [AFSetting QUARTZ_KLINE_H])); // 分时背景框
    CGContextStrokePath(context);
    
    //CGContextMoveToPoint(context, 1, 1);
    //CGContextAddLineToPoint(context, 1, QUARTZ_KLINE_H);
    
    //CGContextMoveToPoint(context, 1, 1);
    //CGContextAddLineToPoint(context, QUARTZ_KLINE_W, 1);
    
    //CGContextMoveToPoint(context, QUARTZ_KLINE_W, 1);
    //CGContextAddLineToPoint(context, QUARTZ_KLINE_W, QUARTZ_KLINE_H);
    //CGContextStrokePath(context);
    
     CGContextSetStrokeColorWithColor(context, AF_K_QUARTZ_DRAW_BORDER_CGCOLOR_daush);  
    // 分时背景虚线
    // 虚线设置
    CGContextSetLineWidth(context, 0.2);
    
   	CGFloat dashPhase = 2.0;
	CGFloat dashPattern[2] = {4.0, 4.0};
	size_t dashCount = 2;
    CGContextSetLineDash(context, dashPhase, dashPattern, dashCount);
    
    // 竖线
    CGContextMoveToPoint(context, [AFSetting QUARTZ_KLINE_W]/4, 1);
    CGContextAddLineToPoint(context, [AFSetting QUARTZ_KLINE_W]/4, [AFSetting QUARTZ_KLINE_H] - 2);
    
    CGContextMoveToPoint(context, [AFSetting QUARTZ_KLINE_W]/2, 1);
    CGContextAddLineToPoint(context, [AFSetting QUARTZ_KLINE_W]/2, [AFSetting QUARTZ_KLINE_H] - 2);
    
    CGContextMoveToPoint(context, [AFSetting QUARTZ_KLINE_W]/4*3, 1);
    CGContextAddLineToPoint(context, [AFSetting QUARTZ_KLINE_W]/4*3, [AFSetting QUARTZ_KLINE_H] - 2);
    
    // 横线
    CGContextMoveToPoint(context, 1, [AFSetting QUARTZ_KLINE_H]/4);
    CGContextAddLineToPoint(context, [AFSetting QUARTZ_KLINE_W] - 2, [AFSetting QUARTZ_KLINE_H]/4);
    
    CGContextMoveToPoint(context, 1, [AFSetting QUARTZ_KLINE_H]/2);
    CGContextAddLineToPoint(context, [AFSetting QUARTZ_KLINE_W] - 2, [AFSetting QUARTZ_KLINE_H]/2);
    
    CGContextMoveToPoint(context, 1, [AFSetting QUARTZ_KLINE_H]/4*3);
    CGContextAddLineToPoint(context, [AFSetting QUARTZ_KLINE_W] - 2, [AFSetting QUARTZ_KLINE_H]/4*3);
    CGContextStrokePath(context);
    
    CGContextStrokePath(context);
    CGContextSetLineDash(context, 0, 0, 0); //取消虚线
}

-(void) drawZhuTai:(CGContextRef)context{
    
    // [S]绘制烛台数据
    //NSArray *localKLindListArray = nil
    //localKLindListArray = [[MarketData data] drawKLineZTListArray];
    if (localKLindListArray== nil) {
        return;
    }

    CGFloat bg_ling_width = 1.0;
    CGContextSetLineWidth(context, bg_ling_width);
    UIColor *mycolor = [AFUtils UIColorFromRGB:0xffffff alpha:1.0];
    CGContextSetStrokeColorWithColor(context, [mycolor CGColor]);
    
    float x;
    float y;
    float x_w;
    float y_h;
    NSString *status = @"";
    NSString *has_hight = @""; // yes/no
    NSString *has_low = @""; // yes/no
    float h_x1,h_y1,h_x2,h_y2;
    float l_x1,l_y1,l_x2,l_y2;
    
    NSDictionary *localItem = nil;
    for(int k = 0; k < [localKLindListArray count]; k++){
        localItem = [localKLindListArray objectAtIndex:k];
        
        x = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"x"]] floatValue];
        y = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"y"]] floatValue];
        x_w = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"x_w"]] floatValue];
        y_h = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"y_h"]] floatValue];
        status = [localItem objectForKey:@"status"]; // up/down
        
        //NSLog(@"status = %@",status);
        
        if ([status isEqualToString:@"up"]) {
            if(y_h == 0){
            CGContextSetStrokeColorWithColor(context, AF_K_QUARTZ_KLINE_UP_CGCOLOR);
            CGContextAddRect(context, CGRectMake(x, y, x_w, y_h));
            CGContextStrokePath(context);
            }else{
            CGContextSetFillColorWithColor(context, AF_K_QUARTZ_KLINE_UP_CGCOLOR);
            CGContextAddRect(context, CGRectMake(x, y, x_w, y_h));
            CGContextFillPath(context);
            
            CGContextSetStrokeColorWithColor(context, AF_K_QUARTZ_KLINE_UP_CGCOLOR);
            }
            
        } else {
            if(y_h == 0){
                CGContextSetStrokeColorWithColor(context, AF_K_QUARTZ_KLINE_DOWN_CGCOLOR);
                CGContextAddRect(context, CGRectMake(x, y, x_w, y_h));
                CGContextStrokePath(context);
            }else{
            CGContextSetFillColorWithColor(context, AF_K_QUARTZ_KLINE_DOWN_CGCOLOR);
            CGContextAddRect(context, CGRectMake(x, y, x_w, y_h));
            CGContextFillPath(context);
    
            CGContextSetStrokeColorWithColor(context, AF_K_QUARTZ_KLINE_DOWN_CGCOLOR);
            }
        }

        // 计算是否需要绘制 最高点
        has_hight = [NSString stringWithFormat:@"%@",[localItem objectForKey:@"has_hight"]];
        //NSLog(@" has_hight = %@",has_hight);
        if ([has_hight isEqualToString:@"yes"]) {
            h_x1 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"h_x1"]] floatValue];
            h_y1 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"h_y1"]] floatValue];
            h_x2 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"h_x2"]] floatValue];
            h_y2 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"h_y2"]] floatValue];
            
            //NSLog(@" h_x1 = %f, h_y1 = %f, h_x2 = %f, h_y2 = %f",h_x1,h_y1,h_x2,h_y2);
            
            CGContextMoveToPoint(context, h_x1, h_y1); // 移动到起点坐标
            // 增加新的坐标点
            CGContextAddLineToPoint(context, h_x2, h_y2);
        }
        
        // 计算是否需要绘制 最低点
        has_low = [NSString stringWithFormat:@"%@",[localItem objectForKey:@"has_low"]];
        if ([has_low isEqualToString:@"yes"]) {
            l_x1 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"l_x1"]] floatValue];
            l_y1 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"l_y1"]] floatValue];
            l_x2 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"l_x2"]] floatValue];
            l_y2 = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"l_y2"]] floatValue];
            
            CGContextMoveToPoint(context, l_x1, l_y1); // 移动到起点坐标
            // 增加新的坐标点
            CGContextAddLineToPoint(context, l_x2, l_y2);
        }
        
        CGContextStrokePath(context);
    }
    // [E]
}


- (void) drawMA:(CGContextRef)context{
    
    if (localMA5ListArray == nil || [localMA5ListArray count] <=0) {
        return;
    }
    NSLog(@" localMA5ListArray = %i, S_DRAW_INDEX = %i", (int)[localMA5ListArray count], S_DRAW_INDEX);
    //int T_DRAW_INDEX = S_DRAW_INDEX + K_MARKET_KLINE_DRAW_COUNT;
    
    if ([localMA5ListArray count] < KLINE_DRAW_COUNT) {
        return;
    }
    
    drawMA5ListArray = [localMA5ListArray subarrayWithRange:NSMakeRange(S_DRAW_INDEX, KLINE_DRAW_COUNT)];
    drawlMA10ListArray = [localMA10ListArray subarrayWithRange:NSMakeRange(S_DRAW_INDEX, KLINE_DRAW_COUNT)];
    drawlMA20ListArray = [localMA20ListArray subarrayWithRange:NSMakeRange(S_DRAW_INDEX, KLINE_DRAW_COUNT)];
    
    NSLog(@" ma5ListArray = %i", (int)[drawMA5ListArray count]);
    
    CGFloat bg_ling_width = 1.0;
    CGContextSetLineWidth(context, bg_ling_width);
    
    NSNumber *closeNum;
    float close = 0;
    float x;
    float y;

    float KLINE_H = [AFSetting QUARTZ_KLINE_H];
    float MIN_W = [AFSetting QUARTZ_KLINE_W]/[AFSetting AF_DRAW_COUNT];
    float MIN_H = [[AFMarketData data] lastKLineMinH];
    float CLOSE_LOW = [[AFMarketData data] lastKLineLow];

//    NSLog(@"%f , %f , %f , %f",KLINE_H,MIN_W,MIN_H,CLOSE_LOW);
    
    
    // MA5 
    if (localMA5ListArray != nil) {
//        UIColor *color_ma5 = [AFUtils UIColorFromRGB:0xffffff alpha:1.0];
        UIColor *color_ma5 = [AFUtils UIColorFromRGB:0xffb945 alpha:1.0];
        CGContextSetStrokeColorWithColor(context, [color_ma5 CGColor]);
        
        int ma5_count = (int)[drawMA5ListArray count];
        
        
        for(int k = 0; k < ma5_count; k++){
            closeNum = [drawMA5ListArray objectAtIndex:k];
            
            close = [closeNum floatValue];
            
            y = KLINE_H - MIN_H*(close - CLOSE_LOW);    // y 最大坐标 － 换算后的高度 ＝ y轴新的坐标
            x = k * MIN_W + MIN_W/2;
           
            
            if(k == 0){
                CGContextMoveToPoint(context, x, y); // 移动到起点坐标
            } else {
                // 增加新的坐标点
                CGContextAddLineToPoint(context, x, y);
            }
            
            last_ma5 = close;
        }
        
        CGContextStrokePath(context);
    }
    
    // MA10
    if (localMA10ListArray != nil) {
//        UIColor *color_ma10 = [AFUtils UIColorFromRGB:0xFFFF00 alpha:1.0];
        UIColor *color_ma10 = [AFUtils UIColorFromRGB:0x65b4ff alpha:1.0];
        CGContextSetStrokeColorWithColor(context, [color_ma10 CGColor]);
        
        int ma10_count = (int)[drawlMA10ListArray count];
        for(int k = 0; k < ma10_count; k++){
            closeNum = [drawlMA10ListArray objectAtIndex:k];
            
            close = [closeNum floatValue];
            
            y = KLINE_H - MIN_H*(close - CLOSE_LOW);    // y 最大坐标 － 换算后的高度 ＝ y轴新的坐标
            x = k * MIN_W + MIN_W/2;

            if(k == 0){
                CGContextMoveToPoint(context, x, y); // 移动到起点坐标
            } else {
                // 增加新的坐标点
                CGContextAddLineToPoint(context, x, y);
            }
            
            last_ma10 = close;
        }
        
        CGContextStrokePath(context);
    }

    // MA20
    if (localMA20ListArray != nil) {
        UIColor *color_ma20 = [UIColor magentaColor];
        CGContextSetStrokeColorWithColor(context, [color_ma20 CGColor]);
        
        int ma20_count = (int)[drawlMA20ListArray count];
        for(int k = 0; k < ma20_count; k++){
            closeNum = [drawlMA20ListArray objectAtIndex:k];
            
            close = [closeNum floatValue];
            
            y = KLINE_H - MIN_H*(close - CLOSE_LOW);    // y 最大坐标 － 换算后的高度 ＝ y轴新的坐标
            x = k * MIN_W + MIN_W/2;
            
            if(k == 0){
                CGContextMoveToPoint(context, x, y); // 移动到起点坐标
            } else {
                // 增加新的坐标点
                CGContextAddLineToPoint(context, x, y);
            }
            
            last_ma20 = close;
        }
        
        CGContextStrokePath(context);
    }
}


#pragma mark - touches event
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    isTouchDown = YES;
    
    if ([self.delegate respondsToSelector:@selector(kLineQuartzViewTouchBegan)]) {
        [self.delegate kLineQuartzViewTouchBegan];
    }
    
    UITouch *touch = [touches anyObject];
    
    startPoint = [touch locationInView:self];
    start_touch_x = startPoint.x;
    
    //[self touchesMoved:touches withEvent:event];
    NSLog(@"startPoint = %f, startPoint = %f ", startPoint.x, startPoint.y);
    
//    float h = self.frame.size.height;
//    
//    // 如果 第一次 touch 的 Y 坐标 小于整个高度的 1/3 则 touch 类型为 十字光标 信息
//    // 否则 为 UIView Move 类型，拖动 显示 KLine
//    if (startPoint.y < h/3) {
//        AF_KLIN_TOUCH_TYPE = AF_KLIN_TOUCH_INFO;
//        
//        [self.afFingerView fingerViewType:AF_K_MARKET_FINGER_VIEW_TYPE_KLINE];
//        self.afFingerView.hidden = NO;
//        
//        move_touch_x = start_touch_x;
//        // 十字光标 信息 刷新
//        [self kLineForAFFingerView];
//    } else {
//        AF_KLIN_TOUCH_TYPE = AF_KLIN_TOUCH_MOVE;
//    }
    
   // 默认 为 UIView Move 类型，拖动 显示 KLine
    AF_KLIN_TOUCH_TYPE = AF_KLIN_TOUCH_MOVE;
    // 长按一秒 没有操作 touch 类型为 十字光标 信息
    islongPress = YES;
    [self performSelector:@selector(changeMoveTouch) withObject:nil afterDelay:0.5];
}

-(void)changeMoveTouch{
    if(islongPress){
        AF_KLIN_TOUCH_TYPE = AF_KLIN_TOUCH_INFO;
        
        [self.afFingerView fingerViewType:AF_K_MARKET_FINGER_VIEW_TYPE_KLINE];
        self.afFingerView.hidden = NO;
        
        move_touch_x = start_touch_x;
        NSLog(@"move_touch_x==%f",move_touch_x);
        // 十字光标 信息 刷新
        [self kLineForAFFingerView];
    }
    
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch  = [touches anyObject];
    currentPoint = [touch locationInView:self];          // n 点坐标
    
    if(fabs(startPoint.x - currentPoint.x) > 3 || fabs(startPoint.y - currentPoint.y) > 3){
        islongPress = NO;
    }else if(islongPress && fabs(startPoint.x - currentPoint.x) <= 3 && fabs(startPoint.y - currentPoint.y) <= 3 && islongPress){
        return;
    }
    
    if (isTouchDown) {
        move_touch_x = currentPoint.x;
        move_touch_y = currentPoint.y;
        NSLog(@"currentPoint.x = %f, currentPoint.y = %f ", currentPoint.x, currentPoint.y);
        
        if (AF_KLIN_TOUCH_TYPE == AF_KLIN_TOUCH_MOVE) {
            [self touchMoveRefreshDrawArray];
        } else {
            // 十字光标 信息 刷新
            [self kLineForAFFingerView];
        }
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    islongPress = NO;
    
    if (AF_KLIN_TOUCH_TYPE == AF_KLIN_TOUCH_INFO) {
        self.afFingerView.hidden = YES;
        [self.afFingerView destioy];
    }
    
    isTouchDown = NO;
    AF_KLIN_TOUCH_TYPE = AF_KLIN_TOUCH_NO;
    
    if ([self.delegate respondsToSelector:@selector(kLineQuartzViewTouchesEnded)]) {
        [self.delegate kLineQuartzViewTouchesEnded];
    }
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //弹框 是 取消 touch
    
    islongPress = NO;
    
    if (AF_KLIN_TOUCH_TYPE == AF_KLIN_TOUCH_INFO) {
        self.afFingerView.hidden = YES;
        [self.afFingerView destioy];
    }
    
    isTouchDown = NO;
    AF_KLIN_TOUCH_TYPE = AF_KLIN_TOUCH_NO;
    
    if ([self.delegate respondsToSelector:@selector(kLineQuartzViewTouchesEnded)]) {
        [self.delegate kLineQuartzViewTouchesEnded];
    }
}
//- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
//
//}

// 十字光标 信息 刷新
-(void)kLineForAFFingerView{
   
    NSLog(@" move_touch_x = %f, VIEW_W = %f ", move_touch_x, VIEW_W);
    
    if (move_touch_x < 0 || move_touch_x > VIEW_W) {
        return;
    }
    
    [self.afFingerView fingerKLineDataRefreshWithX:move_touch_x withY:move_touch_y];

}

#pragma mark - 获取手指移动对应的 数据
-(void) kLineCurrentPointInfo:(float)_x{

    fingerDict = nil;
    prefingerDict = nil;
    
    //float _count = [kLineDrawArray count];
    float _p = [AFSetting QUARTZ_KLINE_W]/[AFSetting AF_DRAW_COUNT];
    
    int _index = floor(_x/_p);
    
    //NSDictionary *itemDict;
    //if (_count > _index) {
    if ([kLineDrawArray count] > _index) {
         fingerDict = [kLineDrawArray objectAtIndex:_index];
        if(_index >= 1){
            prefingerDict = [kLineDrawArray objectAtIndex:(_index - 1)];
        }
    }

    NSNumber *maNum;
    if ([drawMA5ListArray count] > _index) {
        maNum = [drawMA5ListArray objectAtIndex:_index];
        fingerMA5 = [maNum floatValue];
    }

    if ([drawlMA10ListArray count] > _index) {
        maNum = [drawlMA10ListArray objectAtIndex:_index];
        fingerMA10 = [maNum floatValue];
    }

    if ([drawlMA20ListArray count] > _index) {
        maNum = [drawlMA20ListArray objectAtIndex:_index];
        fingerMA20 = [maNum floatValue];
    }
}

-(NSDictionary*)fingerKLine{
    return fingerDict;
}


-(NSDictionary*)prefingerKLine{
    return prefingerDict;
}

-(float)fingerMA5{
    return fingerMA5;
}

-(float)fingerMA10{
    return fingerMA10;
}

-(float)fingerMA20{
    return fingerMA20;
}

@end

